package com.ndp.fb.mongo.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.ndp.fb.util.DateUtil;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@NoArgsConstructor
public class FBAdForMongo implements Serializable {

	private static final long serialVersionUID = -288404003845606110L;

	private Object campaign;
	private Object adSet;
	private Object creative;
	// facebook 返回
	private Long ad_id;
	private Long account_id;
	private Object adlabels;
	private Long adset_id;
	private Integer bid_amount;
	private Object bid_info;
	private String bid_type;
	private String configured_status;
	private Object conversion_specs;
	private String created_time;
	private String effective_status;
	private Long last_updated_by_app_id;
	private String name;
	private Object tracking_specs;
	private String updated_time;
	private Long campaign_id;
	private Object ad_review_feedback;
	private Object failed_delivery_checks;

	private Long updated_utc_time;
	private Long created_utc_time;

	private List<ImageVersion> imageInfo;

	private String platform;

	public static FBAdForMongo getAdGroupForMongo(JSONObject objMap) {
		FBAdForMongo adForMongo = new FBAdForMongo();
		adForMongo.setAccount_id(objMap.getLongValue("account_id"));
		adForMongo.setAd_id(objMap.getLongValue("id"));
		adForMongo.setAd_review_feedback(JSONObject.parseObject(
				objMap.get("ad_review_feedback") == null ? null : objMap.getString("ad_review_feedback"), Map.class));
		adForMongo.setAdlabels(JSONObject
				.parseArray(objMap.get("adlabels") == null ? null : objMap.getString("adlabels"), List.class));

		JSONObject adset = objMap.getJSONObject("adset");
		if (adset != null) {
			adset.put("campaign_id", adset.getLongValue("campaign_id"));
			adset.put("account_id", adset.getLongValue("account_id"));
			adset.put("adset_id", adset.getLongValue("id"));
			adset.put("lifetime_budget", adset.getLongValue("lifetime_budget"));
			adset.put("daily_budget", adset.getLongValue("daily_budget"));
			adset.put("budget_remaining", adset.getLongValue("budget_remaining"));

			adset.remove("id");
			// 生成时间
			Calendar cCreateTime = DateUtil.FbDateToJavaDate(adset.getString("created_time"));
			Calendar cUpdateTime = DateUtil.FbDateToJavaDate(adset.getString("updated_time"));

			Calendar cStartTime = DateUtil.FbDateToJavaDate(adset.getString("start_time"));
			Calendar cEndTime = DateUtil.FbDateToJavaDate(adset.getString("end_time"));

			adset.put("created_time", adset.getString("created_time"));
			adset.put("updated_time", adset.getString("updated_time"));
			adset.put("start_time", adset.getString("start_time"));
			if (cEndTime == null) {
				adset.put("end_time", "");
			}
			adset.put("created_utc_time", cCreateTime.getTimeInMillis());
			adset.put("updated_utc_time", cUpdateTime.getTimeInMillis());
			adset.put("start_utc_time", cStartTime.getTimeInMillis());

			if (cEndTime == null) {
				adset.put("end_utc_time", 0);
			} else {
				adset.put("end_utc_time", cEndTime.getTimeInMillis());
			}
			JSONObject promoted_object = adset.getJSONObject("promoted_object");
			if (promoted_object != null) {
				PromotedObject promotedObject = JSONObject.parseObject(promoted_object.toJSONString(),
						PromotedObject.class);
				adset.put("promoted_object", promotedObject);
				if (StringUtils.isNotBlank(promotedObject.getObject_store_url())) {
					if (promotedObject.getObject_store_url().indexOf("google") > -1) {
						adForMongo.setPlatform("google_play");
					}
					if (promotedObject.getObject_store_url().indexOf("itunes") > -1) {
						adForMongo.setPlatform("itunes");
					}
				}
			}
			adForMongo.setAdSet(adset);
		}

		adForMongo.setAdset_id(objMap.getLongValue("adset_id"));
		adForMongo.setBid_amount(objMap.getIntValue("bid_amount"));
		adForMongo.setBid_info(JSONObject
				.parseObject(objMap.get("bid_info") == null ? null : objMap.getString("bid_info"), Map.class));
		adForMongo.setBid_type(objMap.getString("bid_type"));
		JSONObject campaign = objMap.getJSONObject("campaign");
		if (campaign != null) {
			campaign.put("campaign_id", campaign.getLongValue("id"));
			campaign.remove("id");
			campaign.put("account_id", campaign.getLongValue("account_id"));

			// 生成时间
			// 生成时间
			Calendar cCreateTime = DateUtil.FbDateToJavaDate(campaign.getString("created_time"));
			Calendar cUpdateTime = DateUtil.FbDateToJavaDate(campaign.getString("updated_time"));

			Calendar cStartTime = DateUtil.FbDateToJavaDate(campaign.getString("start_time"));
			Calendar cEndTime = DateUtil.FbDateToJavaDate(campaign.getString("stop_time"));

			campaign.put("created_time", campaign.getString("created_time"));
			campaign.put("updated_time", campaign.getString("updated_time"));
			campaign.put("start_time", campaign.getString("start_time"));
			if (cEndTime == null) {
				campaign.put("end_time", "");
			}
			campaign.put("created_utc_time", cCreateTime.getTimeInMillis());
			campaign.put("updated_utc_time", cUpdateTime.getTimeInMillis());
			campaign.put("start_utc_time", cStartTime.getTimeInMillis());
			if (cEndTime == null) {
				campaign.put("end_utc_time", 0);
			} else {
				campaign.put("end_utc_time", cEndTime.getTimeInMillis());
			}
			adForMongo.setCampaign(campaign);
		}
		// JSONObject creative = objMap.getJSONObject("creative");
		// if(creative!=null){
		// JSONObject object_story_spec =
		// creative.getJSONObject("object_story_spec");
		// if(object_story_spec!=null){
		// if(object_story_spec.getJSONObject("link_data")!=null){
		// JSONObject link_data = object_story_spec.getJSONObject("link_data");
		// if(link_data.getJSONArray("child_attachments")!=null){
		// //轮播图广告
		// }else{
		// //普通广告
		//
		// }
		// }
		// }
		// }
		adForMongo.setCampaign_id(objMap.getLongValue("campaign_id"));
		adForMongo.setConfigured_status(objMap.getString("configured_status"));
		adForMongo.setConversion_specs(JSONObject.parseObject(
				objMap.get("conversion_specs") == null ? null : objMap.getString("conversion_specs"), List.class));

		// 生成时间
		Calendar cCreateTime = DateUtil.FbDateToJavaDate(objMap.getString("created_time"));
		Calendar cUpdateTime = DateUtil.FbDateToJavaDate(objMap.getString("updated_time"));

		adForMongo.setCreated_time(objMap.getString("created_time"));
		adForMongo.setUpdated_time(objMap.getString("created_time"));
		adForMongo.setCreated_utc_time(cCreateTime == null ? null : cCreateTime.getTimeInMillis());
		adForMongo.setUpdated_utc_time(cUpdateTime == null ? null : cUpdateTime.getTimeInMillis());

		adForMongo.setCreative(JSONObject
				.parseObject(objMap.get("creative") == null ? null : objMap.getString("creative"), Map.class));
		adForMongo.setEffective_status(objMap.getString("effective_status"));
		adForMongo.setLast_updated_by_app_id(objMap.getLongValue("last_updated_by_app_id"));
		adForMongo.setName(objMap.getString("name"));
		adForMongo.setTracking_specs(JSONObject.parseObject(
				objMap.get("tracking_specs") == null ? null : objMap.getString("tracking_specs"), List.class));
		adForMongo.setFailed_delivery_checks(JSONObject.parseObject(
				objMap.get("failed_delivery_checks") == null ? null : objMap.getString("failed_delivery_checks"),
				List.class));
		return adForMongo;
	}
}
